/*
 * @Copyright: 2017 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.model.to;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.yyfax.commons.lang.DateUtil;
import com.yyfax.framework.exception.YYException;
import com.yyfax.framework.yyfax.enums.RspCodeEnum;
import com.yyfax.pay.common.enums.PaymentEnum;
import com.yyfax.pay.common.model.to.YYPayParams;

/**
 * 批量代付下单参数
 * 
 * @author jiangmy
 * @date 2017-04-11 13:56:44
 * @since v1.0.0
 */
@SuppressWarnings("serial")
public class PayoutBatchOrderParams extends YYPayParams {
	private String serverIp;
	private String merchantCode; // 商户编号
	private String paymentCode;
	private String orderId; // 商户订单号
	private String orderTime; // 订单时间
	private String expireTime; // 过期时间
	private String batchDate; // 批次日期
	private String batchNo; // 批次号
	private int count; // 总笔数
	private int amount; // 总金额
	private String notifyUrl; // 异步通知url

	private List<PayoutOrder> dataList;

	public String getServerIp() {
		return serverIp;
	}

	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}

	public String getMerchantCode() {
		return merchantCode;
	}

	public void setMerchantCode(String merchantCode) {
		this.merchantCode = merchantCode;
	}

	public String getPaymentCode() {
		return paymentCode;
	}

	public void setPaymentCode(String paymentCode) {
		this.paymentCode = paymentCode;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getOrderTime() {
		return orderTime;
	}

	public void setOrderTime(String orderTime) {
		this.orderTime = orderTime;
	}

	public String getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(String expireTime) {
		this.expireTime = expireTime;
	}

	public String getBatchDate() {
		return batchDate;
	}

	public void setBatchDate(String batchDate) {
		this.batchDate = batchDate;
	}

	public String getBatchNo() {
		return batchNo;
	}

	public void setBatchNo(String batchNo) {
		this.batchNo = batchNo;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	public String getNotifyUrl() {
		return notifyUrl;
	}

	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}

	public List<PayoutOrder> getDataList() {
		return dataList;
	}

	public void setDataList(List<PayoutOrder> dataList) {
		this.dataList = dataList;
	}

	@Override
	public boolean validate() throws Exception {

		if (StringUtils.isBlank(this.serverIp)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'serverIp'不能为空");
		}

		if (StringUtils.isBlank(this.merchantCode)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'merchantCode'不能为空");
		}

		if (StringUtils.isBlank(this.paymentCode) && PaymentEnum.fromValue(this.paymentCode) == null) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'paymentCode'不能为空/不存在相应支付通道");
		}

		if (StringUtils.isBlank(this.orderId)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'orderId'不能为空");
		}

		if (StringUtils.isBlank(this.orderTime)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'orderTime'不能为空");
		} else {
			// 校验时间格式
			if (!DateUtil.vaildFormat(orderTime, DateUtil.FORMAT_DATETIME)) {
				throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'orderTime' 时间格式不正确" + DateUtil.FORMAT_DATETIME);
			}
		}

		if (StringUtils.isBlank(this.notifyUrl)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'notifyUrl'不能为空");
		}

		if (this.amount <= 0) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'amount' 不能小于等于 0 分");
		}

		if (StringUtils.isBlank(this.batchDate)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'batchDate'不能为空");
		} else {
			// 校验时间格式
			if (!DateUtil.vaildFormat(batchDate, DateUtil.FORMAT_DATE)) {
				throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'batchDate' 时间格式不正确" + DateUtil.FORMAT_DATE);
			}
		}

		if (StringUtils.isBlank(this.batchNo)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'batchNo'不能为空");
		}

		if (this.count <= 0) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'count' 不能小于等于 0 分");
		}

		if (dataList == null || dataList.isEmpty()) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'dataList' 不能为空/长度不能为0");
		}

		int sumCount = 0; // 代付订单笔数统计
		int sumAmount = 0; // 代付订单金额统计
		for (PayoutOrder payoutOrder : dataList) {
			payoutOrder.validate();
			sumCount++;
			sumAmount += payoutOrder.getAmount();
		}

		if (sumCount != this.count || sumAmount != amount) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, " 总金额/总笔数不一致");
		}

		return true;
	}

	@Override
	public String[] signFields() {
		return new String[] { "serverIp", "merchantCode", "paymentCode", "userId", "orderId", "orderTime", "expireTime", "batchDate", "batchNo",
				"count", "amount", "notifyUrl" };
	}

	@Override
	public boolean checkSign(String key) throws YYException {
		// 验证详细订单数据
		for (PayoutOrder payoutOrder : dataList) {
			payoutOrder.checkSign(key);
		}

		return true;
	}

	public static class PayoutOrder extends YYPayParams {
		private String orderId;
		private String userId;
		private String payeeCardNo; // 收款
		private String payeeName;
		private int amount; // 收款金额

		public String getOrderId() {
			return orderId;
		}

		public void setOrderId(String orderId) {
			this.orderId = orderId;
		}

		public String getUserId() {
			return userId;
		}

		public void setUserId(String userId) {
			this.userId = userId;
		}

		public String getPayeeCardNo() {
			return payeeCardNo;
		}

		public void setPayeeCardNo(String payeeCardNo) {
			this.payeeCardNo = payeeCardNo;
		}

		public String getPayeeName() {
			return payeeName;
		}

		public void setPayeeName(String payeeName) {
			this.payeeName = payeeName;
		}

		public int getAmount() {
			return amount;
		}

		public void setAmount(int amount) {
			this.amount = amount;
		}

		@Override
		public boolean validate() throws Exception {

			if (StringUtils.isBlank(this.orderId)) {
				throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'orderId'不能为空");
			}

			if (StringUtils.isBlank(this.userId)) {
				throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'userId'不能为空");
			}

			if (StringUtils.isBlank(this.payeeCardNo)) {
				throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'payeeCardNo'不能为空");
			}

			if (StringUtils.isBlank(this.payeeName)) {
				throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'payeeName'不能为空");
			}
			if (this.amount <= 0) {
				throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'amount' 不能小于等于 0 分");
			}

			return true;
		}

		@Override
		public String[] signFields() {
			return new String[] { "orderId", "userId", "payeeCardNo", "payeeName", "amount" };
		}

	}
}
